﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Drawing;
using WcfMedical.DBO;
using WcfMedical.DataAccess;
using System.Web;
using System.IO;
using System.ServiceModel.Activation;
using System.Web.Hosting;

namespace WcfMedical
{
    // REMARQUE : si vous modifiez le nom de classe « ServiceUser » ici, vous devez également mettre à jour la référence à « ServiceUser » dans Web.config.
    public class ServiceUser : IServiceUser
    {
        /// <summary>
        /// crée la liste des 4 users
        /// </summary>
        /// <returns>retourne la liste de users</returns>
        private List<User> CreateListUser()
        {
            StreamReader sr = null;
            BinaryReader read = null;
            
            List<DBO.User> res = new List<WcfMedical.DBO.User>();
            

            DBO.User user1 = new WcfMedical.DBO.User()
            {
                Firstname = "gregory",
                Login = "greg",
                Name = "house",            
                Pwd = "greg",
                Role = "Medecin",
                Connected = false
            };

            sr = new StreamReader(HostingEnvironment.ApplicationPhysicalPath + "\\Pictures\\Users\\medecin.jpg");
            read = new BinaryReader(sr.BaseStream);            
            user1.Picture = read.ReadBytes((int)sr.BaseStream.Length);
            
            DBO.User user2 = new WcfMedical.DBO.User()
            {
                Firstname = "fréderic",
                Login = "fred",
                Name = "ducelier",
                Pwd = "fred",
                Role = "Chirurgien",
                Connected = false
            };
            
            sr = new StreamReader(HostingEnvironment.ApplicationPhysicalPath + "\\Pictures\\Users\\chirurgien.jpg");
            read = new BinaryReader(sr.BaseStream);
            user2.Picture = read.ReadBytes((int)sr.BaseStream.Length);
            
            DBO.User user3 = new WcfMedical.DBO.User()
            {
                Firstname = "Laura",
                Login = "laura",
                Name = "dupont",              
                Pwd = "laura",
                Role = "Infirmière",
                Connected = false
            };
                       
            sr = new StreamReader(HostingEnvironment.ApplicationPhysicalPath + "\\Pictures\\Users\\infirmiere.jpg");
            read = new BinaryReader(sr.BaseStream);
            user3.Picture = read.ReadBytes((int)sr.BaseStream.Length);

            DBO.User user4 = new WcfMedical.DBO.User()
            {
                Firstname = "Albert",
                Login = "albert",
                Name = "Einstein",                
                Pwd = "albert",
                Role = "Radiologue",
                Connected = false
            };
                       
            sr = new StreamReader(HostingEnvironment.ApplicationPhysicalPath + "\\Pictures\\Users\\radiologue.jpg");
            read = new BinaryReader(sr.BaseStream);
            user4.Picture = read.ReadBytes((int)sr.BaseStream.Length);
                       
            res.Add(user1);
            res.Add(user2);
            res.Add(user3);
            res.Add(user4);

            return res;
        }

        #region IServiceUser Membres
        /// <summary>
        /// retourne la liste de users
        /// </summary>
        /// <returns>une liste de users</returns>
        public List<WcfMedical.DBO.User> GetListUser()
        {
            if (!DaSingleton.GetInstance().ListUser.Any())                
            {
               
                DaSingleton.GetInstance().ListUser = CreateListUser();
            }
            return DaSingleton.GetInstance().ListUser;
        }

        /// <summary>
        /// récupère un user selon son login
        /// </summary>
        /// <param name="login">login du user</param>
        /// <returns>un user si il y a un problème une exception est lancée</returns>
        public WcfMedical.DBO.User GetUser(string login)
        {
            if (!DaSingleton.GetInstance().ListUser.Any())
            {
                DaSingleton.GetInstance().ListUser = CreateListUser();
            }
            try
            {
                return DaSingleton.GetInstance().ListUser.Where(x => x.Login == login).First();
            }
            catch (Exception ex)
            {                
                throw new Exception("Exception Security ;)", ex);
            }
        }

        /// <summary>
        /// ajout un user
        /// </summary>
        /// <param name="user">le user</param>
        /// <returns>true si tout s'est bien passé</returns>
        public bool AddUser(User user)
        {
            if (!DaSingleton.GetInstance().ListUser.Any())
            {
                DaSingleton.GetInstance().ListUser = CreateListUser();
            }
            DaSingleton.GetInstance().ListUser.Add(user);
            return true;
        }

        /// <summary>
        /// supprime un user selon son login
        /// </summary>
        /// <param name="login">login de la personne</param>
        /// <returns>true si ca s'est bien passé sinon risque d'avoir une exception</returns>
        public bool DeleteUser(string login)
        {
            if (!DaSingleton.GetInstance().ListUser.Any())
            {
                DaSingleton.GetInstance().ListUser = CreateListUser();
            }
            DaSingleton.GetInstance().ListUser.Remove(DaSingleton.GetInstance().ListUser.Where(x => x.Login == login).First());
            return true;
        }

        /// <summary>
        /// permet de connecter un user
        /// </summary>
        /// <param name="login">son login</param>
        /// <param name="pwd">son mot de passe</param>
        /// <returns>true si ca s'est bien passé sinon false ou une exception</returns>
        public bool Connect(string login, string pwd)
        {
            if (!DaSingleton.GetInstance().ListUser.Any())
            {
                DaSingleton.GetInstance().ListUser = CreateListUser();
            }
            try
            {
                DBO.User tmpUser = DaSingleton.GetInstance().ListUser.Where(x => x.Login == login).FirstOrDefault();
                if (tmpUser == null)
                {
                    return false;
                }
                else
                {
                    tmpUser.Connected = tmpUser.Pwd == pwd;
                    return tmpUser.Pwd == pwd;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("Exception Security ;)", ex);
            }
        }

        /// <summary>
        /// permet de déconnecter un utilisateur
        /// </summary>
        /// <param name="login">son login</param>
        public void Disconnect(string login)
        {
            if (!DaSingleton.GetInstance().ListUser.Any())
            {
                DaSingleton.GetInstance().ListUser = CreateListUser();
            }
            DaSingleton.GetInstance().ListUser.Where(x => x.Login == login).First().Connected = false;
        }

        /// <summary>
        /// permet d'obtenir le role d'un user selon son login
        /// </summary>
        /// <param name="login">le login de l'utilisateur</param>
        /// <returns>son role sous forme de chaine de caractére</returns>
        public string GetRole(string login)
        {
            if (!DaSingleton.GetInstance().ListUser.Any())
            {
                DaSingleton.GetInstance().ListUser = CreateListUser();
            }
            try
            {
                return DaSingleton.GetInstance().ListUser.Where(x => x.Login == login).First().Role;
            }
            catch (Exception ex)
            {
                throw new Exception("Exception Security ;)", ex);
            }
        }
        #endregion
    }
}
